/*
 * @FileName: [UppAdminListener.java]
 * 
 * @Package com.bcinfo.upp.admin.servlet
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of BCINFO
 * Technology Limited Company ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in accordance
 * with the terms of the contract agreement you entered into with RKY.
 * 
 * $Rev: 652 $ $LastChangedDate: 2013-10-31 14:33:24 +0800 (星期四, 31 十月 2013) $
 * $LastChangedBy: wanggc $
 * 
 * @category bcinfo-upp
 * 
 * @version 1.1
 * 
 * @author $Author: wanggc $
 * 
 * Change History:[Formatter: author date description] <br/> 1 2 3
 */

package com.bcinfo.iccard.admin.servlet;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.bcinfo.iccard.admin.common.Constants;
import com.bcinfo.upp.ac.model.Resource;
import com.bcinfo.upp.ac.model.ResourceAction;
import com.bcinfo.upp.ac.service.ResourceService;
import com.bcinfo.upp.exception.UppDataBaseException;

/**
 * 容器启动事件监听，用来启动全局或则缓存数据<br/>
 * <p>
 * Description:<br/>
 * <p>
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *       <listener>
 *     <listener-class>com.bcinfo.upp.admin.servlet.UppAdminListener</listener-class>
 *   </listener>
 * </PRE>
 * <p>
 */
public class UppAdminListener implements ServletContextListener
{

    private final static Logger _logger = Logger.getLogger(UppAdminListener.class);

    /*
     * (non-Javadoc) Description: <br/>
     * 
     * @param sce
     * 
     * @see
     * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
     * .ServletContextEvent)
     */
    @Override
    public void contextInitialized(ServletContextEvent sce)
    {
        // 把所有action数据初始化到map
        _logger.info("把所有action数据初始化到map");
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sce
                .getServletContext());

        ResourceService rs = (ResourceService) webApplicationContext.getBean("resourceService");
        List<Resource> reses = null;
        try
        {
            reses = rs.listFunctions();
        }
        catch (UppDataBaseException e)
        {
            _logger.error("把所有action数据初始化到map" + e);
        }
        catch (Exception e)
        {
            _logger.error("把所有action数据初始化到map失败：" +ExceptionUtils.getStackTrace(e));
        }
        if(reses!=null){
            for (Resource resource : reses)
            {
                List<ResourceAction> actions = resource.getResType().getResourceActions();
                for (ResourceAction resourceAction : actions)
                {
                    Constants.RESOURCE_ACTION_MAP.put(resourceAction.getActionCode(), resourceAction.getBitwiseValue());
                    Constants.RESOURCE_RESOURCE_MAP.put(resourceAction.getActionCode(), resource.getResourceId());
                }

            }
        }
        
    }

    /*
     * (non-Javadoc) Description: <br/>
     * 
     * @param sce
     * 
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
     * ServletContextEvent)
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce)
    {

    }

}
